﻿using Encodings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.IO;

namespace Encodings
{
    

    [TestClass()]
    public class EncodingsTest
    {

        [TestMethod()]
        public void ConvertFromToEncodingTest()
        {
            string fromFilename = @"C:\Users\User\Documents\visual studio 2010\Projects\Exam_DelagatesRegexStringsInputOutput\Encodings\bin\Debug\FileWith1251Encoding.txt";
            string toFileName = "FileWithUTF8Encoding.txt";
            Encodings.ConvertFromToEncoding(fromFilename, toFileName);

            Encoding source = Encoding.GetEncoding("windows-1251");
            Encoding outEnc = Encoding.UTF8;

            StreamReader fromFile = new StreamReader(fromFilename,source);
            StreamReader toFile = new StreamReader(toFileName, outEnc);

            using (fromFile)
            {
                using (toFile)
                {
                    string firstFileLine = fromFile.ReadLine();
                    string secondFileLine = toFile.ReadLine();

                    byte[] utf8Bytes = outEnc.GetBytes(secondFileLine);
                    byte[] bytesIn1251 = Encoding.Convert(outEnc, source, utf8Bytes);

                    string converted = source.GetString(bytesIn1251);

                    Assert.AreEqual(firstFileLine, converted);
                }
            }


        }
    }
}
